﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Reflection;

using AutoDownload.Settings;

namespace AutoDownload.Config.Editor
{
    public partial class MainForm : Form
    {
        ServiceController m_ServiceController;

        AutoDownload.Settings.Config m_ConfigFile;

        public MainForm()
        {
            InitializeComponent();

            RefreshServiceStatus();

            RefreshFeeds();

            m_ConfigFile = AutoDownload.Settings.Config.Load(AutoDownload.Settings.Config.GetConfigFileName());
            if (!File.Exists(m_ConfigFile.SabnzbdINI))
            {
                ShowEditor();
            }

            RebuildLog();
        }

        private void ShowEditor()
        {
            ConfigEditor _CE = new ConfigEditor(m_ConfigFile);
            _CE.ShowDialog();

            if (_CE.ReloadConfig)
            {
                m_ConfigFile = AutoDownload.Settings.Config.Load(AutoDownload.Settings.Config.GetConfigFileName());
            }

            if (!File.Exists(m_ConfigFile.SabnzbdINI))
            {
                MessageBox.Show("Config not loaded shutting down");
                this.Close();
            }
        }

        private void RefreshFeeds()
        {
            //Config m_Config = Config.Load(GetConfigFileName());
            //cbFeeds.Items.Clear();
            //foreach (Feed _F in m_Config.Feeds)
            //{
            //    cbFeeds.Items.Add(_F.Name);
            //}
            //if (cbFeeds.Items.Count > 0)
            //    cbFeeds.SelectedIndex = 0;


        }

        private void RefreshServiceStatus()
        {
            try
            {
                m_ServiceController = new ServiceController("AutoSubDownload");

                switch (m_ServiceController.Status)
                {
                    case ServiceControllerStatus.ContinuePending:
                        btnStart.Enabled = false;
                        btnStop.Enabled = false;
                        btnFetch.Enabled = false;
                        lStatus.Text = "ContinuePending";
                        break;
                    case ServiceControllerStatus.PausePending:
                        btnStart.Enabled = false;
                        btnStop.Enabled = false;
                        btnFetch.Enabled = false;
                        lStatus.Text = "PausePending";
                        break;
                    case ServiceControllerStatus.Paused:
                        btnStart.Enabled = true;
                        btnStop.Enabled = false;
                        btnFetch.Enabled = false;
                        lStatus.Text = "Paused";
                        break;
                    case ServiceControllerStatus.Running:
                        btnStart.Enabled = false;
                        btnStop.Enabled = true;
                        btnFetch.Enabled = true;
                        lStatus.Text = "Running";
                        timerServiceStatus.Enabled = false;
                        break;
                    case ServiceControllerStatus.StartPending:
                        btnStart.Enabled = false;
                        btnStop.Enabled = false;
                        btnFetch.Enabled = false;
                        lStatus.Text = "StartPending";
                        break;
                    case ServiceControllerStatus.StopPending:
                        btnStart.Enabled = false;
                        btnStop.Enabled = false;
                        btnFetch.Enabled = false;
                        lStatus.Text = "StopPending";
                        break;
                    case ServiceControllerStatus.Stopped:
                        btnStart.Enabled = true;
                        btnStop.Enabled = false;
                        btnFetch.Enabled = false;
                        lStatus.Text = "Stopped";
                        timerServiceStatus.Enabled = false;
                        break;
                    default:
                        btnStart.Enabled = false;
                        btnStop.Enabled = false;
                        btnFetch.Enabled = false;
                        lStatus.Text = "Not found";
                        break;
                }

            }
            catch (Exception)
            {
                btnStart.Enabled = false;
                btnStop.Enabled = false;
                btnFetch.Enabled = false;
                lStatus.Text = "Service not found";
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            DisableButtons();
            m_ServiceController.Start();
            timerServiceStatus.Enabled = true;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            DisableButtons();
            m_ServiceController.Stop();
            timerServiceStatus.Enabled = true;
        }

        private void btnFetch_Click(object sender, EventArgs e)
        {
            DisableButtons();

            timerServiceStatus.Enabled = false;
            m_ServiceController.Stop();

            alreadyRestarted = false;
            tmrFetch.Enabled = true;
        }

        private void DisableButtons()
        {
            btnStart.Enabled = false;
            btnStop.Enabled = false;
            btnFetch.Enabled = false;
        }

        private void timerServiceStatus_Tick(object sender, EventArgs e)
        {
            RefreshServiceStatus();
        }

        private void timerLogCheck_Tick(object sender, EventArgs e)
        {
            RebuildLog();

            if (!timerServiceStatus.Enabled)
            {
                RefreshServiceStatus();
            }
        }

        private void RebuildLog()
        {

            AutoDownload.Settings.Config m_Config = AutoDownload.Settings.Config.Load(AutoDownload.Settings.Config.GetConfigFileName());
            lbDownloads.BeginUpdate();
            lbDownloads.Items.Clear();
            for (int i = m_Config.DownoadedSubs.Count - 1; i >= 0; i--)
            {
                lbDownloads.Items.Add(m_Config.DownoadedSubs[i]);
            }
            lbDownloads.EndUpdate();
        }

        //private static string GetConfigFileName()
        //{
        //    string configFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config.xml");
        //    return configFileName;
        //}

        private void cbFeeds_SelectedIndexChanged(object sender, EventArgs e)
        {
            RebuildLog();
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            ShowEditor();

            RefreshFeeds();
        }

        private bool alreadyRestarted = false;
        private void tmrFetch_Tick(object sender, EventArgs e)
        {
            m_ServiceController = new ServiceController("AutoSubDownload");
            if ((m_ServiceController.Status == ServiceControllerStatus.Stopped) && (!alreadyRestarted))
            {
                alreadyRestarted = true;
                m_ServiceController.Start();
            }

            if ((m_ServiceController.Status == ServiceControllerStatus.Running) && (alreadyRestarted))
            {
                RefreshServiceStatus();
                timerServiceStatus.Enabled = true;
            }
        }
    }
}
